﻿using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace EWms.WebApi.Filter
{
    public class ResultFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if (context.HttpContext.Response == null || !(context.Result is ObjectResult result1))
                return;
            Result result2 = new Result(200, result1.Value);
            context.Result = new ContentResult()
            {
                Content = JsonConvert.SerializeObject(result2, Formatting.Indented, new JsonSerializerSettings
                {
                    ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
                    DateFormatString = "yyyy-MM-dd HH:mm:ss"
                }),
                ContentType = "application/json"
            };
        }
    }
}
